package cn.schoolwow.workflow.test.module.assigner.testcase;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.jsonarray.QueryJSONArray;
import cn.schoolwow.util.domain.query.resource.QueryResource;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowAssigner;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.Validate;
import org.junit.Assert;

import java.util.List;

public class StartLeaveAnyApprovalAuditTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setNodes(flowContext);
        startWorkFlow(flowContext);
        departmentLeaderAudit(flowContext);
        checkStatus(flowContext);
    }

    @Override
    public String name() {
        return "启动请假多人或签测试用例";
    }

    private void setNodes(FlowContext flowContext){
        JSONObject workflow = QueryResource.newQuery("请假多人或签部署文件", "workflow/多审批人/请假流程或签审批.json")
                .execute()
                .getAsJSONObject();
        JSONArray nodes = workflow.getJSONArray("nodes");
        flowContext.putCurrentFlowData("nodes", nodes);
    }

    private void startWorkFlow(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        StartWorkFlowRequest startWorkFlowRequest = new StartWorkFlowRequest();
        startWorkFlowRequest.definitionName = "请假流程或签审批";
        startWorkFlowRequest.initiator = "张三";

        WorkFlowInstance workFlowInstance = quickWorkFlow.startWorkFlow(startWorkFlowRequest);
        flowContext.putInstanceData(workFlowInstance);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList.add(workFlowInstance.getId());
        flowContext.putCurrentFlowInstanceData(workFlowTaskQuery);
    }

    private void departmentLeaderAudit(FlowContext flowContext){
        JSONArray nodes = flowContext.checkData("nodes", JSONArray.class);
        WorkFlowTaskQuery workFlowTaskQuery = flowContext.checkInstanceData(WorkFlowTaskQuery.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        JSONObject node = QueryJSONArray.newQuery(nodes)
                .addQuery("name", "部门主管审批")
                .execute()
                .getObject();
        JSONArray assigner = node.getJSONArray("assigner");
        List<WorkFlowAssigner> workFlowAssignerList = assigner.toJavaList(WorkFlowAssigner.class);
        //第一位同意
        {
            String innerAssigner = workFlowAssignerList.get(0).assignerList.get(0);
            workFlowTaskQuery.assignerList.clear();
            workFlowTaskQuery.assignerList.add(innerAssigner);
            WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
            Validate.notNull(workFlowTaskResponse, "审批人["+innerAssigner+"]的任务不存在!");
            quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
        }
        //其他人的审批任务应当查询不到
        {
            for(int i=1;i<workFlowAssignerList.size();i++){
                WorkFlowAssigner workFlowAssigner = workFlowAssignerList.get(i);
                for(int j=0;j<workFlowAssigner.assignerList.size();j++){
                    String innerAssigner = workFlowAssigner.assignerList.get(j);
                    workFlowTaskQuery.assignerList.clear();
                    workFlowTaskQuery.assignerList.add(innerAssigner);
                    long count = quickWorkFlow.getWorkFlowTaskCount(workFlowTaskQuery);
                    if(count>0){
                        throw new IllegalArgumentException("审批人["+innerAssigner+"]的审批任务不应该存在");
                    }
                }
            }
        }
    }

    private void checkStatus(FlowContext flowContext){
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        WorkFlowInstance workFlowInstance1 = quickWorkFlow.getSingleWorkFlowInstance(workFlowInstance.getId());
        Assert.assertEquals(WorkFlowInstanceStatus.Finished.status, workFlowInstance1.getStatus().intValue());
    }

}
